﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
using System.Drawing;
using System.Diagnostics;
using OMC.DataManagement;
using System.Windows.Forms;


// 2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user
//    actions, such as clicking a button. Note: if you have exported this Ribbon from the Ribbon designer,
//    move your code from the event handlers to the callback methods and modify the code to work with the
//    Ribbon extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.


namespace OMC
{
    /// <summary>
    /// The OMC ribbon.
    /// </summary>
    [ComVisible(true)]
    public class Ribbon : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;
        public Manager Manager;
        private SaveFileDialog dialog = new SaveFileDialog();

        /// <summary>
        /// Create a new OMC ribbon.
        /// </summary>
        public Ribbon()
        {
            dialog.Filter = "ARFF (*.arff)|*.arff";
        }

        /// <summary>
        /// Get label for given control.
        /// </summary>
        /// <param name="control">the control</param>
        /// <returns></returns>
        public string GetLabel(Office.IRibbonControl control)
        {
            Debug.Assert(control != null);

            switch (control.Id)
            {
                case "aboutGroup":
                    return OMC.Properties.StringResources.backstageAbout;
                case "aboutProductName":
                    return OMC.Properties.StringResources.productName;
                case "aboutPublishVersion":
                    return OMC.Properties.StringResources.publishVersion;
                case "aboutCodePlexHyperlink":
                    return OMC.Properties.StringResources.backstageAboutCodePlexHyperlink;

                case "optionsOpenLogFile":
                    return OMC.Properties.StringResources.openLogFile;
                case "optionsSaveAsARFF":
                    return OMC.Properties.StringResources.optionsSaveAsARFF;

                case "statusClassifier":
                    {
                        string classifier = OMC.Properties.StringResources.statusClassifier;
                        string loaded = OMC.Properties.StringResources.statusLoaded;
                        string notLoaded = OMC.Properties.StringResources.statusNotLoaded;
                        if (Manager.textClassifier != null)
                        {
                            return classifier + ": " + loaded;
                        }
                        else
                        {
                            return classifier + ": " + notLoaded;
                        }
                    }
                case "statusBuilderClassifier":
                    {
                        string classifierBuilder = OMC.Properties.StringResources.statusClassifierBuilder;
                        string loaded = OMC.Properties.StringResources.statusLoaded;
                        string notLoaded = OMC.Properties.StringResources.statusNotLoaded;
                        if (Manager.textClassifierBuilder != null)
                        {
                            return classifierBuilder + ": " + loaded;
                        }
                        else
                        {
                            return classifierBuilder + ": " + notLoaded;
                        }
                    }
                default:
                    return "";
            }
        }

        /// <summary>
        /// Get image for given control.
        /// </summary>
        /// <param name="control">the control; must not be <code>null</code></param>
        /// <returns></returns>
        public Bitmap GetImage(Office.IRibbonControl control)
        {
            Debug.Assert(control != null);

            switch (control.Id)
            {
                case "aboutImage":
                    return OMC.Properties.StringResources.logoNoShadow;
                default:
                    return null;
            }
        }

        public void OptionsOpenLogFile(Office.IRibbonControl control)
        {
            foreach (string path in Main.LogFilePaths) {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.EnableRaisingEvents = false;
                process.StartInfo.FileName = path;
                process.Start();
            }
        }

        public void OptionsEvaluateClassifier(Office.IRibbonControl control)
        {
            MessageBox.Show(Manager.GetGoodnessExtra());
        }

        public void OptionsSaveAsARFF(Office.IRibbonControl control)
        {
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Manager.textClassifierBuilder.SaveUnfiltered(dialog.FileName);
            }
        }

        public void OptionsRefresh(Office.IRibbonControl control)
        {
            Manager.RefreshAllInThread();
            StatusRefresh(control);
        }

        public void StatusRefresh(Office.IRibbonControl control)
        {
            ribbon.InvalidateControl("statusClassifier");
            ribbon.InvalidateControl("statusBuilderClassifier");
        }

        #region IRibbonExtensibility Members

        /// <summary>
        /// Get 
        /// </summary>
        /// <param name="ribbonID"></param>
        /// <returns></returns>
        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("OMC.Ribbon.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1

        /// <summary>
        /// Load ribbon.
        /// </summary>
        /// <param name="ribbonUI"></param>
        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}
